package com.demo.datastructure.array;

import java.util.*;

/**
 * @description: letcode 15 三数之和
 * @author: ljr
 * @time: 2021/5/2 17:53
 * @since 1.1
 */
public class ThreeSumArray2 {

    public List<List<Integer>> threeSum(int[] nums) {
        int length = nums.length;
        if(length < 3){
             return new ArrayList();
        }
        List<List<Integer>> list = new ArrayList<>();
        if(length == 3){
            if(nums[0]+nums[1]+nums[2] == 0){
                List<Integer> sonList = new ArrayList<>();
                sonList.add(nums[0]);
                sonList.add(nums[1]);
                sonList.add(nums[2]);
                list.add(sonList);
                return list;
            }
            return new ArrayList();
        }

        Arrays.sort(nums);

        int newLength = length-2;
        for(int c = 0; c < newLength; c++) {
            int num1 = nums[c];
            int i = c+1;

            if(num1 > 0){
                break;
            }
            if(c != 0 && num1 == nums[c-1]){
                continue;
            }

            int third = length - 1;
            for (; i < third; i++) {
                int num2 = nums[i];

                if(i != c+1 && num2 == nums[i-1]){ continue; }
                if(num1 + num2 > 0){ break;}

                int k = i + 1;
                for (; k < length; k++) {
                    int num3 = nums[k];
                    if(num3 < 0){
                        continue;
                    }

                    if (num1 + num2 + num3 == 0) {
                        List<Integer> sonList = new ArrayList<>();
                        sonList.add(num1);
                        sonList.add(num2);
                        sonList.add(num3);
                        list.add(sonList);
                        break;
                    }else if(num1 + num2 + num3 > 0){
                        break;
                    }
                }
            }
        }
        return list;
    }


    public static void main(String[] args) {
        ThreeSumArray2 threeSumArray = new ThreeSumArray2();
        int[] nums = new int[]{-1,0,1,2,-1,-4,-2,-3,3,0,4};
        List<List<Integer>> list= threeSumArray.threeSum(nums);
        System.out.println("1");
    }
}
